import {
  Button,
  Form,
  FormItem,
  Input,
  message,
  Modal,
  Radio,
  RadioGroup,
  Space,
  TypographyText
} from 'ant-design-vue'
import { defineComponent, nextTick, onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'

import { recommondApi } from '@/apis'
import { ProblemItem } from '@/apis/recommondTypes'
import { type ProTableProps, type ProTableRef, ProTable } from '@/components'
import { TABLE_FORMAT, TABLE_ITEM_RENDER, TABLE_RENDERER } from '@/constants'
import { useDialog } from '@/hooks'

export default defineComponent({
  name: 'RdQuestionTemp',
  setup() {
    const { showConfirm } = useDialog()

    const tableRef = ref({} as ProTableRef)

    const statusOptions = [
      { label: '禁用', value: 0 },
      { label: '启用', value: 1 }
    ]

    const showDialog = ref(false)
    const dialogLoading = ref(false)

    const optionItemCur = ref({} as ProblemItem)
    const tableConfig = ref({
      fixedHeight: true,
      options: {
        border: true,
        align: 'center',
        toolbarConfig: {
          perfect: false,
          slots: {
            buttons: 'toolbarButtons'
          }
        },

        columns: [
          {
            title: '问题类型',
            field: 'complaintName'
          },
          {
            title: '创建时间',
            field: 'createTime',
            formatter: [TABLE_FORMAT.Date, 'yyyy-MM-dd HH:mm:ss']
          },
          {
            title: '状态',
            fixed: 'right',
            width: 100,
            slots: {
              default: 'status'
            }
          },
          {
            title: '操作',
            fixed: 'right',
            slots: {
              default: 'operation'
            },
            width: 150
          }
        ],

        proxyConfig: {
          form: true,
          props: {
            result: 'data'
          },
          ajax: {
            query: async ({ form }) => {
              const data = await recommondApi.getProblemList()
              return data
            }
          }
        }
      }
    } as ProTableProps)

    //新增
    function add() {
      optionItemCur.value = { complaintStatus: 1 } as ProblemItem
      showDialog.value = true
    }

    //编辑
    function edit(item) {
      Object.assign(optionItemCur.value, item)
      showDialog.value = true
    }

    //编辑/新增确定
    async function dialogConfirm() {
      if (!optionItemCur.value.complaintName) {
        message.error('请输入问题类型')
        return
      }

      try {
        dialogLoading.value = true
        if (optionItemCur.value.id) {
          await recommondApi.editProblem(optionItemCur.value)
        } else {
          await recommondApi.addProblem(optionItemCur.value)
        }
        showDialog.value = false
        tableRef.value.refresh()
      } finally {
        dialogLoading.value = false
      }
    }

    //启用禁用操作
    function handleForbidden(item: ProblemItem) {
      showConfirm({
        content:
          item.complaintStatus == 1
            ? '确认禁用该问题类型？'
            : '确认启用问题类型？',
        onOk: async () => {
          try {
            await recommondApi.upDownProblem(
              item.id,
              item.complaintStatus == 1 ? 0 : 1
            )
            tableRef.value.refresh()
          } catch (error) {}
        }
      })
    }

    return {
      tableRef,
      tableConfig,
      showDialog,
      optionItemCur,
      dialogConfirm,
      add,
      edit,
      handleForbidden
    }
  },
  render() {
    return (
      <div>
        <ProTable
          ref="tableRef"
          v-slots={{
            status: ({ row }) => {
              return (
                <Space>
                  {row.complaintStatus == 0 ? (
                    <TypographyText type="danger">禁用</TypographyText>
                  ) : (
                    <TypographyText type="success">启用</TypographyText>
                  )}
                </Space>
              )
            },
            operation: ({ row }) => (
              <Space>
                <Button
                  type="link"
                  size="small"
                  onClick={() => this.handleForbidden(row)}
                >
                  {row.complaintStatus == 1 ? '禁用' : '启用'}
                </Button>
                <Button type="link" size="small" onClick={() => this.edit(row)}>
                  编辑
                </Button>
              </Space>
            ),
            toolbarButtons: () => {
              return (
                <Space>
                  <Button type="primary" onClick={this.add}>
                    新增
                  </Button>
                </Space>
              )
            }
          }}
          {...this.tableConfig}
        />
        <Modal
          v-model:visible={this.showDialog}
          title="新增"
          destroyOnClose
          width={500}
          onOk={this.dialogConfirm}
        >
          <Form labelCol={{ span: 5 }}>
            <FormItem label={'问题类型'} required>
              <Input
                maxlength={60}
                v-model:value={this.optionItemCur.complaintName}
                placeholder="请输入"
                style={{ width: '300px' }}
              />
            </FormItem>
            <FormItem label="状态：" required>
              <RadioGroup v-model:value={this.optionItemCur.complaintStatus}>
                <Radio value={1}>启用</Radio>
                <Radio value={0}>禁用</Radio>
              </RadioGroup>
            </FormItem>
          </Form>
        </Modal>
      </div>
    )
  }
})
